package com.quectel.cms.controller.partyeducationexam;


import com.google.common.base.Preconditions;
import com.quectel.cms.controller.BaseController;
import com.quectel.cms.interceptor.LoginUserSession;
import com.quectel.cms.util.SessionHolder;
import com.quectel.constant.core.partyeducationexam.PartyEducationQuestionBankConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.constant.sys.SysGroupConstants;
import com.quectel.core.module.partyeducationexam.dto.PartyEducationQuestionBankDto;
import com.quectel.core.module.partyeducationexam.service.PartyEducationQuestionBankService;
import com.quectel.sys.module.sys.dto.SysGroupDto;
import com.quectel.sys.module.sys.service.SysGroupService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author ryan
 * @email ryan.ran@quectel.com
 * @date 2023/06/16 15:12
 */
@RestController
@RequestMapping("partyEducationQuestionBank")
@Tag(name = "PartyEducationQuestionBankController", description = "题库相关api")
public class PartyEducationQuestionBankController extends BaseController {

    @DubboReference
    private PartyEducationQuestionBankService partyEducationQuestionBankService;
    @DubboReference
    private SysGroupService sysGroupService;

    @GetMapping("queryAll")
    @RequirePermissions(values = "partyEducationQuestionBank:list")
    @Operation(summary = "条件查询列表")
    public Response<List<PartyEducationQuestionBankDto>> queryAll() {
        Map<String, Object> params = Request.parseParams(httpServletRequest);
        super.fillAuthorityData(params);
        List<PartyEducationQuestionBankDto> bankDtos = partyEducationQuestionBankService.queryList(params);
        return Response.<List<PartyEducationQuestionBankDto>>ok().wrap(bankDtos);
    }

    /**
     * 分页查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    @RequirePermissions(values = "partyEducationQuestionBank:list")
    @Operation(summary = "分页获取题库列表")
    public Response<Page<PartyEducationQuestionBankDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());
        int total = partyEducationQuestionBankService.queryTotal(request.getParams());

        List<PartyEducationQuestionBankDto> list = null;
        if (total > 0) {
            list = partyEducationQuestionBankService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        LoginUserSession sysUser = SessionHolder.getSysUser();
        SysGroupDto sysGroupDto = sysGroupService.selectById(sysUser.getGroupId());
        Preconditions.checkNotNull(sysGroupDto);
        for (PartyEducationQuestionBankDto partyEducationQuestionBankDto : list) {
            partyEducationQuestionBankDto.setOwned(PartyEducationQuestionBankConstants.OwnedEnum.NOT_OWNING.getOwnedStatus());
            if (SysGroupConstants.IsPartyManagerEnum.IS_MANAGER.getIsPartyManager().equals(sysGroupDto.getIsPartyManager())
                    && partyEducationQuestionBankDto.getPartyId().equals(sysGroupDto.getPartyId())) {
                partyEducationQuestionBankDto.setOwned(PartyEducationQuestionBankConstants.OwnedEnum.OWNED.getOwnedStatus());
            }
        }

        Page<PartyEducationQuestionBankDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<PartyEducationQuestionBankDto>>ok().wrap(page);
    }

    /**
     * 编辑查询反显
     *
     * @param id
     * @return
     */
    @GetMapping("infoForEdit/{id}")
    @RequirePermissions(values = "partyEducationQuestionBank:edit")
    @Operation(summary = "在编辑时根据id查询题库详情")
    public Response<PartyEducationQuestionBankDto> infoForEdit(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (partyEducationQuestionBankService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        PartyEducationQuestionBankDto partyEducationQuestionBankDto = partyEducationQuestionBankService.selectById(id);

        return Response.<PartyEducationQuestionBankDto>ok().wrap(partyEducationQuestionBankDto);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "partyEducationQuestionBank:info")
    @Operation(summary = "根据id查询题库的详情")
    public Response<PartyEducationQuestionBankDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (partyEducationQuestionBankService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        PartyEducationQuestionBankDto partyEducationQuestionBankDto = partyEducationQuestionBankService.selectById(id);

        return Response.<PartyEducationQuestionBankDto>ok().wrap(partyEducationQuestionBankDto);
    }

    /**
     * 保存
     *
     * @param partyEducationQuestionBankDto
     * @return
     */
    @PostMapping("save")
    @RequirePermissions(values = "partyEducationQuestionBank:edit")
    @Operation(summary = "新增题库")
    public Response<Object> save(@RequestBody @Validated PartyEducationQuestionBankDto partyEducationQuestionBankDto) {

        //填充基础数据
        fillSaveCommonData(partyEducationQuestionBankDto);

        partyEducationQuestionBankService.saveAndUpdate(partyEducationQuestionBankDto);

        return Response.ok();
    }


    /**
     * 修改
     *
     * @param partyEducationQuestionBankDto
     * @return
     */
    @PostMapping("update")
    @RequirePermissions(values = "partyEducationQuestionBank:edit")
    @Operation(summary = "根据id修改题库")
    public Response<Object> update(@RequestBody  @Validated PartyEducationQuestionBankDto partyEducationQuestionBankDto) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(partyEducationQuestionBankDto.getId()));
        if (partyEducationQuestionBankService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        //填充基础数据
        fillEditCommonData(partyEducationQuestionBankDto);

        partyEducationQuestionBankService.saveAndUpdate(partyEducationQuestionBankDto);

        return Response.ok();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("delete")
    @RequirePermissions(values = "partyEducationQuestionBank:delete")
    @Operation(summary = "根据多条记录id批量删除题库")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (partyEducationQuestionBankService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        partyEducationQuestionBankService.deleteBatch(ids);

        return Response.ok();
    }
}
